#first look at some timing stuff
#16 feb 2013. cmf.

#note, typo in one line of subject 2008 -- timestamp had a space in it, 
#so when split on space to convert to csv, lost some info. fixed it in master csv, nowhere else

##################################################################################################
#clear workspace, load packages
rm(list=ls())
require(lattice)
require(doBy)
require(reshape)

##################################################################################################
#read in data and clean it up a little
data = read.csv('DYNaming_AllData.csv')
data$Timestamp = as.character(data$Timestamp) #default is to read in as factor; change to character
data$Timestamp = gsub('#', '', data$Timestamp) #strip off the '#' that surrounds the timestamps

##################################################################################################
#i wanna convert the timestamps to one column of milliseconds

#first two characters are always 0 in this dataset, so just delete em.
data$Timestamp = substr(data$Timestamp, 4,10) #first two characters are always 0 in this dataset, so just delete em.

#extract last character (1/10 of second), convert to number x 100
lastNums_ms = as.integer(substr(data$Timestamp, 7,7)) * 100

#seconds *1000
seconds_ms = as.integer(substr(data$Timestamp,4,5 )) * 1000

#minutes * 60000
minutes_ms = as.integer(substr(data$Timestamp,1,2)) * 60000

#add em all together to get running ms
ms = minutes_ms + seconds_ms + lastNums_ms

#bind to dataframe
data = cbind(data, ms)

rm(lastNums_ms); rm(minutes_ms); rm(ms); rm(seconds_ms)

##################################################################################################
#create vector showing me temporal spacing between each utterance
Lag1Diffs = diff(data$ms)
Lag1Diffs[Lag1Diffs < 0] = 0  #replace negative values with 0
Lag1Diffs = c(0, Lag1Diffs)   #add 0 to the beginning, so each entry in Lag1Diffs tells me delay since previous utterance
Lag1Diffs_s = Lag1Diffs/1000  #easier to think about spacing in terms of seconds
data = cbind(data, Lag1Diffs_s) #bind to dataframe

rm(Lag1Diffs); rm(Lag1Diffs_s)

##################################################################################################
#first look
hist(data$Lag1Diffs_s)
range(data$Lag1Diffs_s) #goes from 0 to 4.63 min....

#guh, let's bin 'em into 500ms
data$Lag1Bins = c()
data$Lag1Bins[data$Lag1Diffs_s == 0] = 0
data$Lag1Bins[data$Lag1Diffs_s > 0 & data$Lag1Diffs_s <=1] = 1
data$Lag1Bins[data$Lag1Diffs_s > 1 & data$Lag1Diffs_s <=5] = 5
data$Lag1Bins[data$Lag1Diffs_s > 5 & data$Lag1Diffs_s <=10] = 10
data$Lag1Bins[data$Lag1Diffs_s > 10 & data$Lag1Diffs_s <=15] = 15
data$Lag1Bins[data$Lag1Diffs_s > 15 & data$Lag1Diffs_s <=20] = 20
data$Lag1Bins[data$Lag1Diffs_s > 20 & data$Lag1Diffs_s <=25] = 25
data$Lag1Bins[data$Lag1Diffs_s > 25 & data$Lag1Diffs_s <=30] = 30
data$Lag1Bins[data$Lag1Diffs_s > 30 & data$Lag1Diffs_s <=35] = 35
data$Lag1Bins[data$Lag1Diffs_s > 35 & data$Lag1Diffs_s <=40] = 40
data$Lag1Bins[data$Lag1Diffs_s > 40 & data$Lag1Diffs_s <=45] = 45
data$Lag1Bins[data$Lag1Diffs_s > 45 & data$Lag1Diffs_s <=50] = 50
data$Lag1Bins[data$Lag1Diffs_s > 50 & data$Lag1Diffs_s <=55] = 55
data$Lag1Bins[data$Lag1Diffs_s > 55 & data$Lag1Diffs_s <=60] = 60
data$Lag1Bins[data$Lag1Diffs_s > 60] = 61

#plot it
hist(data$Lag1Bins, xlab = '5 Second Bins', main = "Delay between utterances, \n in 5 second bins up to 1 minute")

#see the table...   about 3/4 of the data has lag of 15 sec or less. bulk is 1-5 sec.
table(data$Lag1Bins)

#get the age group right so it displays in plot
data$AgeGroup = as.character(data$AgeGroup)
data$AgeGroup[data$AgeGroup == '4'] = "04"
data$AgeGroup[data$AgeGroup == '8'] = "08"

#plot Lag1Bins by age group. remarkably similar....
histogram(~data$Lag1Bins | data$AgeGroup, layout = c(1,5), col = 'gray', main = 'Delay between utterances', xlab = '5 sec bins, up to 1 minute')
histogram(~data$Lag1Bins | data$AgeGroup + data$Familiarity, col = 'red', main = 'Delay between utterances', xlab = '5 sec bins, up to 1 minute')


#below here, 18 feb2013
###########################################################################################
#delay between subsequent utterances....

#look at by kid histos, then look at by-kid median histos, then do mean of medians

#first: get rid of FirstUtterancesPerKid
#don't need "0" delay contributing to analysis
Firsts = which(data$Lag1Diffs_s == 0)
DataNoFirsts = data[-Firsts,]
rm(Firsts)

#overall histogram
hist(DataNoFirsts$Lag1Diffs_s, main = "Delay between subsequent utterances \n Whole Corpus", xlab = 'Delay (seconds)', ylim = c(0,1000))

#get histos by kid
DataNoFirsts$SubjectID = as.character(DataNoFirsts$SubjectID)
histogram(~DataNoFirsts$Lag1Diffs_s | DataNoFirsts$SubjectID, col = 'gray', main = 'By Subject: Delay between subsequent utterances \n cut off at 100 seconds', xlab = 'Delay (seconds)', xlim = c(0,100))

#compute median, by kid and familiarity
#eg, if utterance X is {novel, familiar}, how much time has gone by since the previous label? 
#this is NOT conditioned on the familiarity status of the previous label
DelayMedians_ByKid = summaryBy(Lag1Diffs_s ~ SubjectID + Familiarity, data = DataNoFirsts, FUN = function(x) { median(x) } )
colnames(DelayMedians_ByKid) = c('Subject', 'Familiarity', 'MedianLag1Diffs')
#add age group column
DelayMedians_ByKid$AgeGroup = c('12', '12', '12', '12', '12', '12', '12', '12', '16', '16', '16', '16', '16', '16', '16', '16', '20', '20', '20', '20', '20', '20', '20', '20','04','04', '04', '04', '04','04', '04', '04','08', '08', '08', '08', '08', '08', '08', '08')

#take a look, by subject & familiarity
#ok there is one 12mo old who is just weird -- subject 1201 --very few utterances of Novel words and one of the delays is way long
histogram(~DelayMedians_ByKid$MedianLag1Diffs | DelayMedians_ByKid$Familiarity + DelayMedians_ByKid$AgeGroup, main = '1201 weirdo subject, scale is warped here \n Median Delay between utterances', xlab = 'Median Delay (seconds)')

#so, remove kid 1201 and plot again
#remove sub 1201 from DelayMedians_ByKid
DelayMedians_ByKid = DelayMedians_ByKid[3:40,]  #INDEX HARDCODED HERE. NOT OPTIMAL...FIX LATER

#display histo again, without subject 1201
histogram(~DelayMedians_ByKid$MedianLag1Diffs | DelayMedians_ByKid$Familiarity + DelayMedians_ByKid$AgeGroup, main = 'use this one \n Median Delay between utterances', xlab = 'Median Delay (seconds)')


#get mean of medians, by age group
DelayMeanOfMedians = summaryBy(MedianLag1Diffs ~ AgeGroup + Familiarity, data = DelayMedians_ByKid, FUN = function(x) { mean(x) } )
colnames(DelayMeanOfMedians) = c('AgeGroup', 'Familiarity', 'MeanOfMedians_DelayLag1')

#reshape for barplot
Familiars = DelayMeanOfMedians[DelayMeanOfMedians$Familiarity == 'Familiar',]
Novels = DelayMeanOfMedians[DelayMeanOfMedians$Familiarity == 'Novel',]
together = cbind(Familiars$MeanOfMedians_DelayLag1, Novels$MeanOfMedians_DelayLag1)
colnames(together) = c('Familiar', 'Novel')
togetherT = t(together)


#plot mean of medians
barplot(togetherT, beside = TRUE, names = Familiars$AgeGroup, ylim = c(0,8), col = c('green4', 'purple4'), xlab = 'Age Group', ylab = "Mean of by-subject medians", main = 'Delay between subsequent utterances')
legend ('topleft', c('Familiar', 'Novel'), col = c('green4', 'purple4'), pch = 15)
